#include "TableStructs.h"
#include "MemCacheTemplateSingleton.h"

using namespace std;

!!types = {}!!
!!formats = {}!!
!!sqltypes = {}!!
!!entry types!!
!!entry bools!!
!!travel!!
!!formats[@name] = '%d'!!
!!types[@name] = 'bool'!!
!!sqltypes[@name] = @sqltype!!
!!leave!!
!!leave!!
!!entry ushorts!!
!!travel!!
!!formats[@name] = '%d'!!
!!types[@name] = 'short'!!
!!sqltypes[@name] = @sqltype!!
!!leave!!
!!leave!!
!!entry ints!!
!!travel!!
!!formats[@name] = '%d'!!
!!types[@name] = 'int'!!
!!sqltypes[@name] = @sqltype!!
!!leave!!
!!leave!!
!!entry int64s!!
!!travel!!
!!formats[@name] = '%lld'!!
!!types[@name] = 'int64'!!
!!sqltypes[@name] = @sqltype!!
!!leave!!
!!leave!!
!!entry doubles!!
!!travel!!
!!formats[@name] = '%f'!!
!!types[@name] = 'double'!!
!!sqltypes[@name] = @sqltype!!
!!leave!!
!!leave!!
!!entry strings!!
!!travel!!
!!formats[@name] = '%s'!!
!!types[@name] = 'string'!!
!!sqltypes[@name] = @sqltype!!
!!leave!!
!!leave!!
!!entry enums!!
!!travel!!
!!formats[@name] = '%d'!!
!!types[@name] = 'enum'!!
!!sqltypes[@name] = @sqltype!!
!!leave!!
!!leave!!
!!leave!!

!!entry tables!!
!!travel!!
!!tableName = @name!!
!!$tableName!!* !!$tableName!!::Allocate()
{
	return ::Allocate<!!$tableName!!>();
}
void !!$tableName!!::Free()
{
	::Free(this);
}
std::string !!$tableName!!::GetHeadString()
{
!!entry fields!!
	return std::string("!!travel!!!!if pumpid > 0:!!!!inc indent!!,!!dec indent!!!!@name!!!!leave!!\n");
!!leave!!
}
std::string !!$tableName!!::CreateDuckdbTableSqlString()
{
	return "CREATE TABLE IF NOT EXISTS t_!!$tableName!!(!!entry fields!!!!travel!!!!if types[@type] == 'string':!!!!inc indent!!!!@name!! varchar, !!dec indent!!!!else:!!!!inc indent!!!!sqltype = sqltypes[@type]!!!!@name!! !!$sqltype!!, !!dec indent!!!!leave!!!!leave!!!!entry primarykey!!PRIMARY KEY(!!travel!!!!if pumpid > 0:!!!!inc indent!!, !!dec indent!!!!@name!!!!leave!!)!!leave!!);!!entry indexes!!!!travel!! CREATE INDEX IF NOT EXISTS !!@name!!Index ON t_!!$tableName!!(!!travel!!!!if pumpid > 0:!!!!inc indent!!, !!dec indent!!!!@name!!!!leave!!);!!leave!!!!leave!!";
}
std::string !!$tableName!!::CreateSqliteTableSqlString()
{
	return "CREATE TABLE IF NOT EXISTS t_!!$tableName!!(!!entry fields!!!!travel!!!!sqltype = sqltypes[@type]!!!!@name!! !!$sqltype!!, !!leave!!!!leave!!!!entry primarykey!!PRIMARY KEY(!!travel!!!!if pumpid > 0:!!!!inc indent!!, !!dec indent!!!!@name!!!!leave!!)!!leave!!); !!entry indexes!!!!travel!!CREATE INDEX IF NOT EXISTS !!@name!!Index ON t_!!$tableName!!(!!travel!!!!if pumpid > 0:!!!!inc indent!!, !!dec indent!!!!@name!!!!leave!!);!!leave!!!!leave!!";
}
std::string !!$tableName!!::CreateMysqlTableSqlString()
{
	return "CREATE TABLE IF NOT EXISTS t_!!$tableName!!(!!entry fields!!!!travel!!!!sqltype = sqltypes[@type]!!!!@name!! !!$sqltype!!, !!leave!!!!leave!!!!entry indexes!!!!travel!!INDEX !!@name!!(!!travel!!!!if pumpid > 0:!!!!inc indent!!, !!dec indent!!!!@name!!!!leave!!), !!leave!!!!leave!!!!entry primarykey!!PRIMARY KEY(!!travel!!!!if pumpid > 0:!!!!inc indent!!, !!dec indent!!!!@name!!!!leave!!)!!leave!!) ENGINE=MyISAM DEFAULT COLLATE='utf8mb4_bin';";
}
std::string !!$tableName!!::TruncateDuckdbSqlString()
{
	return "Delete From t_!!$tableName!!;";
}
std::string !!$tableName!!::TruncateSqliteSqlString()
{
	return "Delete From t_!!$tableName!!;";
}
std::string !!$tableName!!::TruncateMysqlSqlString()
{
	return "Truncate Table t_!!$tableName!!;";
}

int !!$tableName!!::ToCsvString(char* buff) const
{
!!entry fields!!
	return sprintf(buff, "\n(!!travel!!!!if pumpid > 0:!!!!inc indent!!,!!dec indent!!!!format = formats[@type]!!'!!$format!!'!!leave!!),",
		!!travel!!!!type = types[@type]!!!!if pumpid > 0:!!!!inc indent!!, !!dec indent!!!!if type == 'enum':!!!!inc indent!!(int)!!dec indent!!!!@name!!!!leave!!);
!!leave!!
}
int !!$tableName!!::ToDebugString(char* buff) const
{
!!entry fields!!
	return sprintf(buff, "\n(!!travel!!!!if pumpid > 0:!!!!inc indent!!, !!dec indent!!!!format = formats[@type]!!!!@name!!:'!!$format!!'!!leave!!),",
		!!travel!!!!type = types[@type]!!!!if pumpid > 0:!!!!inc indent!!, !!dec indent!!!!if type == 'enum':!!!!inc indent!!(int)!!dec indent!!!!@name!!!!leave!!);
!!leave!!
}
int !!$tableName!!::GetSqlString(char* buff) const
{
!!entry fields!!
	return sprintf(buff, "\n(!!travel!!!!if pumpid > 0:!!!!inc indent!!, !!dec indent!!!!format = formats[@type]!!'!!$format!!'!!leave!!),",
		!!travel!!!!type = types[@type]!!!!if pumpid > 0:!!!!inc indent!!, !!dec indent!!!!if type == 'enum':!!!!inc indent!!(int)!!dec indent!!!!@name!!!!leave!!);
!!leave!!
}

!!leave!!
!!leave!!
